home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Diamond Collection / The Diamond Collection (Software Vault)(Digital Impact).ISO / cdr47 / lzpip103.zip / ZIPPIPE.C < prev   
Text File  |  1994-10-30  |  7KB  |  257 lines

  1. #include <stdio.h>
  2. #include "modern.h"
  3. #include "lzpipe.h"
  4. #include "zalloc.h"
  5. #include "oscode.h"
  6. #include "crc32.h"
  7. #define __ALLOCEXT__
  8. #include "zipguts.h"
  9. #ifndef UNIX
  10. #    ifdef M_XENIX
  11. #        define UNIX
  12. #    endif
  13. #    ifdef unix
  14. #        define UNIX
  15. #    endif
  16. #endif
  17. #ifdef UNIX
  18. #    include <time.h>
  19. #endif
  20. #ifdef MSDOS
  21. #    include <dos.h>
  22. #    ifdef __TURBOC__
  23. #        include <io.h>
  24. #    endif
  25. #endif
  26.  
  27. static unsigned long dostime __ARGS__((void))
  28. {
  29. #ifdef MSDOS
  30. # ifdef __TURBOC__
  31.    union { struct date d; struct time t; } u;
  32.    struct ftime f;
  33.  
  34.    getdate(&u.d);
  35.    f.ft_year  = u.d.da_year - 1980;
  36.    f.ft_month = u.d.da_mon;
  37.    f.ft_day   = u.d.da_day;
  38.  
  39.    gettime(&u.t);
  40.    f.ft_hour  = u.t.ti_hour;
  41.    f.ft_min   = u.t.ti_min;
  42.    f.ft_tsec  = u.t.ti_sec;
  43.  
  44.    return *(unsigned long *)&f;
  45. # else
  46.    uninon REGS r;
  47.    unsigned d;
  48.  
  49.    r.h.ah = 0x2A; /* get date */ intdos(&r, &r);
  50.    d = ((r.x.cx - 1980) << 9) | (r.h.dh << 5) | r.h.dl;
  51.  
  52.    r.h.ah = 0x2C; /* get time */ intdos(&r, &r);
  53.    return ((unsigned long)d << 16) |
  54.       (r.h.ch << 11) | (r.h.cl << 5) | (r.h.dh >> 1);
  55. # endif
  56. #else
  57. # ifdef UNIX
  58.    extern long time();
  59.    struct tm *s;
  60.    long t;
  61.  
  62.    (void)time(&t);
  63.    s = localtime(&t);
  64.    if (s->tm_year < 80) return 0L;
  65.    return ((unsigned long)(s->tm_year - 80) << 25) |
  66.       ((unsigned long)s->tm_mon << 21) | ((unsigned long)s->tm_mday << 16) |
  67.       ((unsigned)s->tm_hour << 11) | (s->tm_min << 5) | (s->tm_sec >> 1);
  68. # else
  69.    /* dummy time stamp */ return 0L;
  70. # endif
  71. #endif
  72. }
  73.  
  74. #ifndef zalloc
  75. /* Turbo C malloc() does not allow dynamic allocation of 64K bytes
  76.  * and farmalloc(64K) returns a pointer with nonzero offset, so we
  77.  * must fix the pointer. Warning: the pointer must be saved in its
  78.  * original form in order to free it, use farfree().
  79.  * For MSC, use halloc instead of this function.
  80.  */
  81. void far *zalloc(void far **p, unsigned n, unsigned s)
  82. {
  83.    register unsigned long l;
  84.    l = (unsigned long)(*p = farmalloc((unsigned long)n*s + 15));
  85.    return (void far *)
  86.       ((0xffff0000L & l) + (0xffff0000L & (((0xffffL & l) + 15) << 12)));
  87. }
  88. #endif
  89.  
  90. int zipalloc()
  91. {
  92. #ifdef DYN_ALLOC
  93.    if (ct_alloc() != 0) return ZNOMEM;
  94.    if (lm_alloc() != 0) {
  95.       ct_free(); return ZNOMEM;
  96.    }
  97. #endif
  98.    return 0;
  99. }
  100.  
  101. void zipfree()
  102. {
  103. #ifdef DYN_ALLOC
  104.    lm_free();
  105.    ct_free();
  106. #endif
  107. }
  108.  
  109. #define putword(x) bi_putsh(x)
  110. static int putlong __ARGS__((ulg));
  111.  
  112. static int putlong(l)
  113. ulg l;
  114. {
  115.    return (putword((unsigned)l)!=0 ||
  116.            putword((unsigned)(l >> 16))!=0) ? ZWRITE : 0;
  117. }
  118.  
  119. static ulg inpsize;
  120. #ifdef DEBUG
  121.        ulg isize;
  122. #endif
  123. static int ziptype;
  124. static ulg timestamp;
  125. static ush flags;
  126.  
  127. /* speed options for the general purpose bit flag */
  128. #define FAST 4
  129. #define SLOW 2
  130.  
  131. int zipcreat(out_port, ztype, dlevel)
  132. #ifdef LZFILE
  133.     FILE *out_port;
  134. #else
  135.     int (*out_port)__ARGS__((int));
  136. #endif
  137. int ztype, dlevel;
  138. {
  139.    register k;
  140.  
  141.    if (dlevel<1  || dlevel>9 || (ztype!=ZIP_PKW && ztype!=ZIP_GNU))
  142.       return (lzerror = ZUNSUP);
  143.    deflate_level = dlevel;
  144.    flags = dlevel <= 1 ? FAST : dlevel >= 9 ? SLOW : 0;
  145.    ziptype = ztype;
  146.  
  147.    crcbegin();
  148.    bi_init();
  149.    if (ct_init() != 0) return (lzerror = ZNOMEM);
  150.    if ((k=lm_init()) != 0) {
  151. #ifdef DYN_ALLOC
  152.       ct_free();
  153. #endif
  154.       return (lzerror = k);
  155.    }
  156.    zip_out_port = out_port;
  157.    /* Write the header to the gzip file */
  158.    /* No extra field, file name or comment; no encryption */
  159.    if (ziptype == ZIP_GNU) {
  160.       if (putword(GZIP_MAGIC) != 0 || /* magic header */
  161.           putbyte(DEFLATED) == EOF || /* compression method */
  162.           putbyte(0) == EOF        || /* general flags: nothing */
  163.           putlong(0L) != 0         || /* dummy time stamp */
  164.           putbyte((int)flags)==EOF || /* extra flags */
  165.           putbyte(OS_CODE) == EOF)    /* OS identifier */
  166.          return (lzerror = ZWRITE);
  167.    } else {
  168.       if (putlong(PKW_LOCAL) != 0 || /* magic header */
  169.           putword(19)        != 0 || /* version to extract */
  170.           putword(flags|=8)  != 0 ||
  171.           putword(DEFLATED)  != 0 || /* compression method */
  172.       /* Who, the hell, needs in this time stamp? */
  173.           putlong(timestamp = dostime()) != 0 ||
  174.           putlong(0L) != 0 || /* dummy CRC */
  175.           putlong(0L) != 0 || /* dummy compressed size */
  176.           putlong(0L) != 0 || /* dummy original size */
  177.           putlong(0L) != 0)   /* null file name & extra fields */
  178.          return (lzerror = ZWRITE);
  179.    }
  180.    inpsize = 0L;
  181.    return 0;
  182. }
  183.  
  184. int zipwrite(buffer, length)
  185. char *buffer; unsigned length;
  186. {
  187.    register k;
  188.  
  189.    if (!zip_out_port) return (lzerror=ZNOPEN, -1);
  190.    if (length) {
  191.       updcrc((unsigned char *)buffer, length);
  192.       inpsize += length;
  193.    }
  194.    k = deflate_level > 3 ?
  195.           lazy_deflate(buffer, length) :
  196.           fast_deflate(buffer, length);
  197.    if (k == -1) lzerror = ZWRITE;
  198.    return k;
  199. }
  200.  
  201. long zipclose()
  202. {
  203.    extern unsigned minlookahead;
  204.    register long l = -1;
  205.    ulg clen, crc;
  206.  
  207.    minlookahead = 0; /* indicate end of input */
  208.    /* Flush out any remaining bytes */
  209.    if (zipwrite(NULL,0) != 0) goto end;
  210.    clen = (compressed_len >> 3);
  211.    crc = getcrc();
  212.    if (ziptype == ZIP_GNU) {
  213.       /* Write the crc. & uncompressed size */
  214.       if (putlong(crc) != 0 || putlong(inpsize) != 0) {
  215.          lzerror = ZWRITE; goto end;
  216.       }
  217.       l = 10 + clen + 8;
  218.    } else {
  219.       /* Write the /data descriptor/ extended local header */
  220.       if (putlong(PKW_EXT)    != 0 || /* signature */
  221.           putlong(crc)        != 0 || /* CRC */
  222.           putlong(clen)       != 0 || /* compressed size */
  223.           putlong(inpsize)    != 0 || /* uncompressed size */
  224.       /* Write the central directory entry */
  225.           putlong(PKW_CENTRAL)!= 0 ||
  226.           putword((OS_CODE<<8)|20) != 0 || /* version made by */
  227.           putword(19)         != 0 || /* version to extract */
  228.           putword(flags)      != 0 ||
  229.           putword(DEFLATED)   != 0 || /* compression method */
  230.           putlong(timestamp)  != 0 ||
  231.           putlong(crc)        != 0 || /* CRC */
  232.           putlong(clen)       != 0 || /* compressed size */
  233.           putlong(inpsize)    != 0 || /* original size */
  234.           putlong(0L)         != 0 || /* filename & extra field length */
  235.           putword(0)          != 0 || /* file comment length */
  236.           putword(0)          != 0 || /* disk number start */
  237.           putword(0)          != 0 || /* internal file attributes */
  238.           putlong(0L)         != 0 || /* external file attributes */
  239.           putlong(0L)         != 0 || /* relative offset of local header */
  240.       /* Finish the central directory */
  241.           putlong(PKW_END)    != 0 ||
  242.       putlong(0L)         != 0 || /* disk numbers - don't care */
  243.       putword(1) != 0 || /* total number of CD entries on this disk */
  244.       putword(1) != 0 || /* total number of CD entries */
  245.           putlong(46L)        != 0 || /* size of the central directory */
  246.       putlong(30+clen+16) != 0 || /* offset of start of CD */
  247.           putword(0) != 0) { /* zipfile comment length */
  248.          lzerror = ZWRITE; goto end;
  249.       }
  250.       l = 30 + clen + 84;
  251.    }
  252. end:
  253.    zip_out_port = NULL;
  254.    zipfree();
  255.    return l;
  256. }
  257.